// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import Flower from "./flower";

const { ccclass, property } = cc._decorator;

@ccclass
export default class flowerpot extends cc.Component {
  // LIFE-CYCLE CALLBACKS:

  // onLoad () {}

  @property(cc.Prefab)
  flowerPrefab1: cc.Prefab = null;
  @property(cc.Prefab)
  flowerPrefab2: cc.Prefab = null;
  @property(cc.Prefab)
  flowerPrefab3: cc.Prefab = null;
  @property(cc.Prefab)
  flowerPrefab4: cc.Prefab = null;
  @property(cc.Prefab)
  flowerPrefab5: cc.Prefab = null;
  @property(cc.Prefab)
  flowerPrefab6: cc.Prefab = null;
  @property(cc.Prefab)
  flowerPrefab7: cc.Prefab = null;
  @property(cc.Prefab)
  flowerPrefab8: cc.Prefab = null;
  @property(cc.Prefab)
  flowerPrefab9: cc.Prefab = null;

  flowerArr: Array<cc.Prefab> = [];

  // 统一的花朵数据结构
  private flowerData = [
    // 第一层数据
    [
      [2, 0, 9],
      [4, 5, 6],
      [3, 0, 2],
      [7, 6, 4],
      [0, 3, 9],
      [6, 3, 2],
      [5, 4, 9],
      [2, 5, 0],
      [2, 0, 2],
    ],
    // 第二层数据
    [
      [4, 5, 6],
      [0, 3, 9],
      [7, 6, 4],
      [2, 0, 9],
      [6, 3, 2],
      [3, 0, 2],
      [2, 0, 2],
      [2, 5, 0],
      [5, 4, 9],
    ],
    // 第三层数据
    [
      [2, 0, 2],
      [2, 0, 9],
      [4, 5, 6],
      [3, 0, 2],
      [7, 6, 4],
      [0, 3, 9],
      [6, 3, 2],
      [5, 4, 9],
      [2, 5, 0],
    ],
  ];

  // start() {
  //   const pengWaiArr = cc.find("Canvas/mainNode/pengWaiNode").children;
  //   for (let i = 0; i < pengWaiArr.length; i++) {
  //     // for (let i = 0; i < 1; i++) {
  //     const pengziNode = pengWaiArr[i];
  //     // 生成随机的2或3
  //     const count = Math.floor(Math.random() * 2) + 2;
  //     let flowerArr = [];
  //     for (let j = 0; j < count; j++) {
  //       // const idx = this.getRandomIdx();
  //       const flo = this.getRandomFlower();
  //       const floNode = cc.instantiate(flo);
  //       flowerArr.push(floNode);
  //       floNode["typeName"] = flo.name;
  //       // const floNode = cc.instantiate(flo);
  //       // floNode["typeName"] = flo.name;
  //       // flowerArr.push(floNode);
  //     }

  //     if (count == 3) {
  //       // 处理3朵花的情况
  //       for (let index = 0; index < count; index++) {
  //         this.placeFlower(flowerArr[index], pengziNode.children[index]);
  //       }
  //     } else {
  //       // 处理2朵花的情况
  //       const idxArr = this.getRandomTwoNumbers();
  //       idxArr.forEach((idx, index) => {
  //         this.placeFlower(flowerArr[index], pengziNode.children[idx]);
  //       });
  //     }

  //     // if (count == 3) {
  //     //   for (let index = 0; index < count; index++) {
  //     //     const posNode = pengziNode.children[index];
  //     //     this.node.addChild(flowerArr[index], 1, flowerArr[index].name);
  //     //     const floWorldPos = posNode.parent.convertToWorldSpaceAR(
  //     //       posNode.getPosition()
  //     //     );
  //     //     flowerArr[index].getComponent(Flower).pengPotNode = posNode;
  //     //     flowerArr[index].setRotation(posNode.rotation);
  //     //     const localPos =
  //     //       flowerArr[index].parent.convertToNodeSpaceAR(floWorldPos);
  //     //     flowerArr[index].setPosition(localPos);
  //     //     posNode["hasFlower"] = true;
  //     //     posNode["flowerNode"] = flowerArr[index];
  //     //   }
  //     // } else {
  //     //   const idxArr = this.getRandomTwoNumbers();
  //     //   idxArr.forEach((idx, index) => {
  //     //     const posNode = pengziNode.children[idx];
  //     //     this.node.addChild(flowerArr[index], 1, flowerArr[index].name);
  //     //     const floWorldPos = posNode.parent.convertToWorldSpaceAR(
  //     //       posNode.getPosition()
  //     //     );
  //     //     flowerArr[index].getComponent(Flower).pengPotNode = posNode;
  //     //     const localPos =
  //     //       flowerArr[index].parent.convertToNodeSpaceAR(floWorldPos);
  //     //     flowerArr[index].setPosition(localPos);
  //     //     flowerArr[index].setRotation(posNode.rotation);
  //     //     posNode["hasFlower"] = true;
  //     //     posNode["flowerNode"] = flowerArr[index];
  //     //   });
  //     // }
  //   }
  // }

  start() {
    // 初始化花朵预制体数组
    this.flowerArr = [
      this.flowerPrefab1,
      this.flowerPrefab2,
      this.flowerPrefab3,
      this.flowerPrefab4,
      this.flowerPrefab5,
      this.flowerPrefab6,
      this.flowerPrefab7,
      this.flowerPrefab8,
      this.flowerPrefab9,
    ];

    const pengWaiArr = cc.find("Canvas/mainNode/pengWaiNode").children;
    cc.log("生成花朵------开始");
    for (let i = 0; i < pengWaiArr.length; i++) {
      const pengziNode = pengWaiArr[i];
      const floorNum = pengziNode["floor"] || 3; // 默认为第3层
      cc.log("生成花朵------floorNum", floorNum, pengziNode["floor"]);
      const layerIndex = floorNum - 1;
      cc.log("生成花朵------layerIndex", layerIndex);
      const pengNo = pengziNode["pengNo"] || i;

      // 确保索引在有效范围内
      // const flowerDataIndex = Math.min(
      //   i,
      //   this.flowerData[layerIndex].length - 1
      // );
      const flowerDataIndex = pengNo - 1;
      cc.log("生成花朵------flowerDataIndex", flowerDataIndex);

      // 获取当前花盆的花朵数据
      const flowerPositionData = this.flowerData[layerIndex][flowerDataIndex];
      cc.log("生成花朵------flowerPositionData", flowerPositionData);

      // 创建花朵并放置到位置
      for (let pos = 0; pos < 3; pos++) {
        // 获取花朵类型索引
        const flowerTypeIndex = flowerPositionData[pos];
        cc.log("生成花朵------flowerTypeIndex", flowerTypeIndex);
        if (flowerTypeIndex == 0) continue;
        // 确保索引在有效范围内
        // const safeIndex = flowerTypeIndex % this.flowerArr.length;
        // 获取对应的花朵预制体
        // const flowerPrefab = this.flowerArr[safeIndex];
        const flowerPrefab = this.flowerArr[flowerTypeIndex - 1];
        cc.log("生成花朵------flowerPrefab", flowerPrefab);
        // 实例化花朵
        const floNode = cc.instantiate(flowerPrefab);
        floNode["typeName"] = flowerPrefab.name;

        // 放置花朵
        this.placeFlower(floNode, pengziNode.children[pos]);
      }
    }
  }
  /**
   * 将花朵放置到指定位置
   * @param flowerNode 花朵节点
   * @param posNode 位置节点
   */
  placeFlower(flowerNode: cc.Node, posNode: cc.Node) {
    // 添加到节点树中
    this.node.addChild(flowerNode, 1, flowerNode.name);

    // 计算世界坐标和本地坐标
    const floWorldPos = posNode.parent.convertToWorldSpaceAR(
      posNode.getPosition()
    );
    const localPos = flowerNode.parent.convertToNodeSpaceAR(floWorldPos);

    // 设置花朵属性
    flowerNode.zIndex = posNode.parent["floor"];
    flowerNode.getComponent(Flower).pengPotNode = posNode;
    flowerNode.getComponent(Flower).pengNo = posNode.parent["pengNo"];
    flowerNode.getComponent(Flower).floor = posNode.parent["floor"];

    // 设置触摸属性
    if (posNode.parent["floor"] == 3) {
      flowerNode.getComponent(Flower).canTouch = true;
    } else {
      flowerNode.getComponent(Flower).canTouch = false;
    }

    // 设置透明度和位置
    flowerNode.opacity = posNode.parent.opacity;
    flowerNode.zIndex = posNode.parent["floor"];
    flowerNode.setPosition(localPos);
    flowerNode.setRotation(posNode.rotation);

    // 更新位置节点属性
    posNode["hasFlower"] = true;
    posNode["flowerNode"] = flowerNode;

    // 添加调试信息
    // console.log(
    //   `花朵 ${flowerNode.getComponent(Flower)} 已加载，canTouch初始值: ${
    //     flowerNode.getComponent(Flower).canTouch
    //   }`
    // );
  }
  /**
   * 返回0,1,2中随机两个不重复的数字
   * @returns 包含两个不重复数字的数组
   */
  getRandomTwoNumbers(): number[] {
    // 创建原始数组[0,1,2]
    const numbers = [0, 1, 2];
    // 结果数组
    const result: number[] = [];

    // 随机选择第一个数字
    const firstIndex = Math.floor(Math.random() * numbers.length);
    result.push(numbers[firstIndex]);
    // 从原数组中移除已选数字
    numbers.splice(firstIndex, 1);

    // 随机选择第二个数字
    const secondIndex = Math.floor(Math.random() * numbers.length);
    result.push(numbers[secondIndex]);

    return result;
  }
  getRandomFlower() {
    const array = [this.flowerPrefab1, this.flowerPrefab2, this.flowerPrefab3];
    const randomIndex = Math.floor(Math.random() * array.length);
    return array[randomIndex];
  }
}
